﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================
using System;
using System.ComponentModel;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Design;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraTreeList.Columns;
using XCI.Windows.Helper;

namespace XCI.Windows.Plugin.GridSetting.UI
{
    [ToolboxItem(false)]
    public partial class BaseGridExpression : XtraUserControl
    {
        protected GridControl MainGrid => gridControl;
        protected GridView MainView => gridView1;
        
        public BaseGridExpression()
        {
            InitializeComponent();
            propertyGrid1.PropertyGrid.OptionsView.ShowRootCategories = false;
        }

        protected virtual FormatConditionCollectionBase GetFormats()
        {
            return null;
        }

        protected virtual TreeListColumnCollection GetTreeColumns()
        {
            return null;
        }

        protected virtual GridColumnCollection GetGridColumns()
        {
            return null;
        }

        protected virtual StyleFormatConditionBase NewCondition()
        {
            return null;
        }
         
        protected void Initialize()
        {
            LoadFiledName();
            MainGrid.DataSource = GetFormats();
        }

        protected void LoadFiledName()
        {
            comBoxColumn.Items.BeginUpdate();
            comBoxColumn.Items.Clear();
            comBoxColumn.Items.Add(new ImageComboBoxItem(null, null, -1));
            var gridColumns = GetGridColumns();
            if (gridColumns != null)
            {
                foreach (GridColumn col in gridColumns)
                {
                    string text = col.Caption;
                    if (string.IsNullOrEmpty(text)) text = col.FieldName;
                    comBoxColumn.Items.Add(new ImageComboBoxItem(text, col, -1));
                }
            }

            var treeColumns = GetTreeColumns();
            if (treeColumns != null)
            {
                foreach (TreeListColumn col in treeColumns)
                {
                    string text = col.Caption;
                    if (string.IsNullOrEmpty(text)) text = col.FieldName;
                    comBoxColumn.Items.Add(new ImageComboBoxItem(text, col, -1));
                }
            }

            comBoxColumn.Items.EndUpdate();
        }

        /// <summary>
        /// 显示表达式编辑器
        /// </summary>
        /// <param name="condition">条件表达式</param>
        protected void ShowEditor(StyleFormatConditionBase condition)
        {
            using (ExpressionEditorForm form = new ConditionExpressionEditorForm(condition, null))
            {
                form.StartPosition = FormStartPosition.CenterParent;
                if (form.ShowDialog(this) == DialogResult.OK)
                {
                    condition.Expression = form.Expression.Replace(" ", "");
                }
            }
        }

        private void SetObject()
        {
            StyleFormatConditionBase condition = gridView1.GetRow(gridView1.FocusedRowHandle) as StyleFormatConditionBase;
            if (condition != null)
            {
                propertyGrid1.SetObject(condition.Appearance);
                propertyGrid1.Enabled = true;
                btnEdit.Enabled = btnDelete.Enabled = true;
            }
            else
            {
                propertyGrid1.PropertyGrid.SelectedObject = null;
                propertyGrid1.Enabled = false;
                btnEdit.Enabled = btnDelete.Enabled = false;
            }
        }

        private void gridViewExpression_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            SetObject();
            btnEdit.Enabled = btnDelete.Enabled = gridView1.FocusedRowHandle > -1;
        }

        private void gridViewExpression_DoubleClick(object sender, EventArgs e)
        {
            btnEdit.PerformClick();
        }

        private void gridView1_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        {
            if (gridView1.OptionsView.ShowIndicator && e.Info.IsRowIndicator && e.RowHandle >= 0)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
                e.Info.ImageIndex = -1;
            }
        }

        private void btnNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var condition = NewCondition();
            ShowEditor(condition);
            if (string.IsNullOrEmpty(condition.Expression))
            {
                MainView.FormatConditions.Remove(condition);
            }
            MainGrid.RefreshDataSource();
            MainView.MoveLastVisible();
        }

        private void btnEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            StyleFormatConditionBase condition = gridView1.GetRow(gridView1.FocusedRowHandle) as StyleFormatConditionBase;
            if (condition != null)
            {
                ShowEditor(condition);
            }
        }

        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (MessageBoxHelper.ShowWarnYesNo("确定要删除选中的数据吗？") == DialogResult.Yes)
            {
                gridView1.DeleteSelectedRows();
            }
        }

        private void editSearchBox_EditValueChanged(object sender, EventArgs e)
        {
            gridView1.ApplyFindFilter(((ButtonEdit)sender).Text.Trim());
            SetObject();
        }

        private void gridView1_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            if (gridView1.SelectedRowsCount > 1 || gridView1.SelectedRowsCount == 0)
            {
                gridView1.ClearSelection();
                gridView1.SelectRow(gridView1.FocusedRowHandle);
            }
        }
    }
}
